{# SPDX-FileCopyrightText: © Fossology contributors
 
   SPDX-License-Identifier: FSFAP
#}
{% extends "include/base.html.twig" %}

{% block styles %}
  {{ parent() }}
  <link rel='stylesheet' href='css/access-token.css'>
  <link rel='stylesheet' href='css/bootstrap-icons.css'>
{% endblock %}

{% block content %}
  <form name="{{ formName }}" method="POST">
  <input type="hidden" name="user_pk" value="{{ userId }}"/>
  {%  if isSessionAdmin %}
    {{ "Select the user to edit"|trans }}:
    {% import "include/macros.html.twig" as macro %}
    {{ macro.select('userid', allUsers, 'userid', userId, ' class="ui-render-select2" onchange="RefreshPage(this.value);"') }}
  {% endif %}

  <table style="border:1px solid black; border-collapse: collapse;" width="100%">
    <tr class="classic">
      <th width="25%">{{ "Username."|trans }}</th>
      <td><input type="text" value="{{ userName|e }}" name="user_name" size="20" {%- if not isSessionAdmin or userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Description (name, contact, or other information)."|trans }} {{ "This may be blank."|trans }}</th>
      <td><input type="text" value="{{ userDescription|e }}" name="user_desc" size="60" {%- if userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Email address."|trans }} {{ "This may be blank."|trans }}</th>
      <td><input type="text" value="{{ userEMail|e }}" name="user_email" size="60" {%- if userDescReadOnly == "true" -%}readonly="true"{%- endif -%}></td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "E-mail notification on job completion"|trans }}</th>
      <td><input type="checkbox" name="email_notify" {% if eMailNotification %}checked{% endif %}></td>
    </tr>
    {% if isSessionAdmin %}
      <tr class="classic">
        <th width="25%">{{ "Select the user's access level."|trans }}</th>
        <td>{{ macro.select('user_perm', allAccessLevels, 'user_perm', accessLevel) }}</td>
      </tr>
      <tr class="classic">
        <th width="25%">{{ "Select the user's status."|trans }} </th>
        <td>{{ macro.select('user_status', allUserStatuses, 'user_status', userStatus) }}</td>
      </tr>
      <tr class="classic">
        <th width="25%">{{ "Select the user's top-level folder. Access is restricted to this folder."|trans }}</th>
        <td><select name="root_folder_fk" class="ui-render-select2">
          {{ folderListOption }}
        </select></td>
      </tr>
    {% endif %}
      <tr class="classic">
        <th width="25%">{{ "Select the user's default folder. Root for Upload and Browse will be this folder."|trans }}</th>
        <td><select name="default_folder_fk" class="ui-render-select2">
          {{ folderListOption2 }}
        </select></td>
      </tr>
    {% if isSessionAdmin %}
      {% if passwordPolicy is empty %}
      <tr class="classic">
        <th width="25%">{{ "Require no password."|trans }}</th>
        <td><input type="checkbox" name="_blank_pass" id="blank_pass" {% if isBlankPassword %}checked="checked" disabled="disabled"{% endif %}/></td>
      </tr>
      {% endif %}
    {% endif %}
    <title>Toggle Password Visibility</title>
    <tr class="classic">
      <th width="25%">{{ "Password."|trans }}</th>
      <td><input type="password" name="_pass1" size="20" id="passcheck" onchange="togglePasswords();"/><span class="bi bi-eye"></span><img src="images/eyes.png" class="bi bi-eye-slash" id="togglePasswords"></img>{% if passwordPolicy is not empty %}
      <br /><span class="passPolicy">{{ passwordPolicy }}</span>
      {% endif %}</td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ "Re-enter password."|trans }}</th>
      <td><input type="password" name="_pass2" size="20" id="pass2"/></td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ ('Default visibility') |trans }}</th>
      <td>
        <input type="radio" name="public" value="private" {% if uploadVisibility == "private" %} checked="checked" {% endif %}/> {{ 'Visible only for active group'| trans }}<img src="images/info_16.png" title="{{ 'which is the currently selected group'|trans }}" alt="" class="info-bullet"/><br/>
        <input type="radio" name="public" value="protected" {% if uploadVisibility == "protected" %} checked="checked" {% endif %}/> {{ 'Visible for all groups'| trans }}<img src="images/info_16.png" title="{{ 'which are accessible by you now'|trans }}" alt="" class="info-bullet"/><br/>
        <input type="radio" name="public" value="public" {% if uploadVisibility == "public" %} checked="checked" {% endif %}/> {{ 'Make Public'| trans }}<img src="images/info_16.png" title="{{ 'visible for all users'|trans }}" alt="" class="info-bullet"/><br/>
      </td>
    </tr>
    <tr class="classic">
      <th width='25%'>{{ 'Default agents selected when uploading data.'|trans }}</th>
      <td>{{ agentSelector }}</td>
    </tr>
    <tr class="classic">
      <th width="25%">{{ "Default bucket pool"| trans }}</th>
      <td>{{ bucketPool }}</td>
    </tr>
    {%  if isSessionAdmin %}
      <tr class="classic">
        <th width="25%">{{ "Select user's default group."|trans }}</th>
        <td><select name="default_group_fk" class="ui-render-select2">
          {{ defaultGroupOption }}
        </select></td>
      </tr>
    {% endif %}
  </table>
  <input type="submit" name="UpdateBtn" value="{{ 'Update Account'|trans }}"/>
  </form>
  <hr>
  <h3>{{ 'REST API Tokens'|trans }}</h3>
  <p>
    {{ 'You can create/manage your tokens here for using FOSSology\'s REST API.'|trans }}
  </p>
  {% if newToken is not empty %}
  <h3>{{ 'New token'|trans }}</h3>
  <table style="border:1px solid black; border-collapse: collapse;" width="100%">
    <tr class="classic">
      <th style="width:20%">
        {{ 'New token for Bearer authentication'|trans }}
      </th>
      <td>
        <textarea readonly style="width:100%">{{ newToken }}</textarea>
      </td>
    </tr>
  </table>
  {% endif %}
  {% if newClient is not empty %}
  <h3>{{ 'New Client'|trans }}</h3>
  <table style="border:1px solid black; border-collapse: collapse;" width="100%">
    <tr class="classic">
      <td style="width:20%">
        {{ newClient|trans }}
      </td>
    </tr>
  </table>
  {% endif %}
  {% if resttoken == "both" or resttoken == "oauth" %}
  <h3>{{ 'Add new oauth client'|trans }}</h3>
  <form name="oauth-token" method="POST">
    <table style="border:1px solid black; border-collapse: collapse;" width="100%">
      <tbody>
        <tr class="classic">
          <th width="20%">
            {{ 'Name'|trans }}
          </th>
          <td>
            <input type="text" name="client_name" size="30" id="client_name"
              placeholder="{{ 'Friendly name for client'|trans }}" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Client ID'|trans }}
          </th>
          <td>
            <input type="text" name="client_id" size="30" id="client_id"
              placeholder="{{ 'Client ID from OAuth'|trans }}" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Access scope'|trans }}
          </th>
          <td>
            <table style="border:0px solid white;border-collapse:collapse" width="100%">
              <tbody>
                <tr>
                  <td>
                    <input type="radio" name="client_scope" value="r"
                      id="client_scope_r" checked="checked" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="client_scope_r">
                      <div>
                        {{ 'Read only access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Limited only to "GET" calls'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% if writeAccess %}
                <tr>
                  <td>
                    <input type="radio" name="client_scope" value="w" id="client_scope_w" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="client_scope_w">
                      <div>
                        {{ 'Read/Write access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Required for calls other than "GET"'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% endif %}
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    <input type="hidden" name="new_client" value="true" />
    <input type="submit" value="{{ 'Add new client'|trans }}" />
  </form>
  {% if clientList is not empty %}
    <hr>
    <h3>{{ 'Active clients '|trans }}({{ clientList|length }})</h3>
    <table class="active-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Client name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%" colspan="2"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleClient in clientList %}
        <tr>
          <td>
            {{ singleClient.name }}
          </td>
          <td>
            {{ singleClient.created }}
          </td>
          <td>
            {%if singleClient.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleClient.id }}" />
          </td>
          <td>
            <input type="button" class="pat-action pat-revoke" value="Revoke"
              id="revoke-{{ singleClient.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  {% if revokedClientList is not empty %}
    <hr>
    <h3>{{ 'Revoked Clients '|trans }}({{ revokedClientList|length }})</h3>
    <table class="expired-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Client name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleClient in revokedClientList %}
        <tr>
          <td>
            {{ singleClient.name }}
          </td>
          <td>
            {{ singleClient.created }}
          </td>
          <td>
            {%if singleClient.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleClient.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  {% endif %} {# End for resttoken #}
  {% if resttoken == "both" or resttoken == "token" %}
  <hr />
  <h3>{{ 'Create a new token'|trans }}</h3>
  <form name="personal-access-token" method="POST">
    <table style="border:1px solid black; border-collapse: collapse;" width="100%">
      <tbody>
        <tr class="classic">
          <th width="20%">
            {{ 'Name'|trans }}
          </th>
          <td>
            <input type="text" name="pat_name" size="30" id="pat_name"
              placeholder="{{ 'Friendly name for new token'|trans }}" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Expires at'|trans }}
          </th>
          <td>
            <input type="text" name="pat_expiry" id="pat_expiry"
              placeholder="YYYY-MM-DD" required="true" />
          </td>
        </tr>
        <tr class="classic">
          <th width="20%">
            {{ 'Access scope'|trans }}
          </th>
          <td>
            <table style="border:0px solid white;border-collapse:collapse" width="100%">
              <tbody>
                <tr>
                  <td>
                    <input type="radio" name="pat_scope" value="r"
                      id="pat_scope_r" checked="checked" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="pat_scope_r">
                      <div>
                        {{ 'Read only access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Limited only to "GET" calls'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% if writeAccess %}
                <tr>
                  <td>
                    <input type="radio" name="pat_scope" value="w" id="pat_scope_w" />
                  </td>
                  <td style="width:100%">
                    <label class="font-weight:bold" for="pat_scope_w">
                      <div>
                        {{ 'Read/Write access'|trans }}
                      </div>
                      <div style="font-weight:lighter;color:#777">
                        {{ 'Required for calls other than "GET"'|trans }}
                      </div>
                    </label>
                  </td>
                </tr>
              {% endif %}
              </tbody>
            </table>
          </td>
        </tr>
      </tbody>
    </table>
    <input type="hidden" name="new_pat" value="true" />
    <input type="submit" value="{{ 'Create new token'|trans }}" />
  </form>
  {% if tokenList is not empty %}
    <hr>
    <h3>{{ 'Active access tokens '|trans }}({{ tokenList|length }})</h3>
    <table class="active-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Token name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Expiry'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%" colspan="2"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleToken in tokenList %}
        <tr>
          <td>
            {{ singleToken.name }}
          </td>
          <td>
            {{ singleToken.created }}
          </td>
          <td>
            {{ singleToken.expire }}
          </td>
          <td>
            {%if singleToken.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleToken.id }}" />
          </td>
          <td>
            <input type="button" class="pat-action pat-revoke" value="Revoke"
              id="revoke-{{ singleToken.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  {% if expiredTokenList is not empty %}
    <hr>
    <h3>{{ 'Expired access tokens '|trans }}({{ expiredTokenList|length }})</h3>
    <table class="expired-token-table">
      <thead>
        <tr>
          <th>
            {{ 'Token name'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Created on'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Expired'|trans }}
          </th>
          <th style="width:15%">
            {{ 'Scope'|trans }}
          </th>
          <th style="width:15%"></th>
        </tr>
      </thead>
      <tbody>
        {% for singleToken in expiredTokenList %}
        <tr>
          <td>
            {{ singleToken.name }}
          </td>
          <td>
            {{ singleToken.created }}
          </td>
          <td>
            {{ singleToken.expire }}
          </td>
          <td>
            {%if singleToken.scope == 'r' %}
              {{ 'read only'|trans }}
            {% else %}
              {{ 'read/write'|trans }}
            {% endif %}
          </td>
          <td>
            <input type="button" class="pat-action pat-reveal" value="Reveal"
              id="reveal-{{ singleToken.id }}" />
          </td>
        </tr>
        {% endfor %}
      </tbody>
    </table>
  {% endif %}
  {% endif %} {# End for resttoken #}
  <br /><br /><br />
{% endblock %}

{% block foot %}
  {{ parent() }}
  <script language="javascript">
    function RefreshPage(val) {
      var uri = '{{ refreshUri }}' + '&newuser=' + val ;
      window.location.assign(uri);
    }

    {% if isBlankPassword %}
    $(document).ready(function(){
      $("#passcheck").keyup(function (){
        var isBlank = ($(this).val()==='');
        $("#blank_pass").prop("checked", isBlank);
      });
    });
    {% endif %}

    $(function() {
      $("#pat_expiry").datepicker({
        minDate : 0,
        maxDate : "+{{ maxTokenDate }}D",
        dateFormat : "yy-mm-dd",
        showOtherMonths : true,
        selectOtherMonths : true,
        showButtonPanel : true
      });
    });
  </script>
  <script src="scripts/user-edit.js" type="text/javascript"></script>
  <script type="text/javascript">{% include "password-policy-check.js.twig" %}</script>
  <script src="scripts/togglePasswords.js" type="text/javascript"></script>
{% endblock %}
